import { PLATFORM_TYPE } from "../../constant/platform"
import { PlatformNotSupportError } from "../errors"
import { AccountUser } from "./accountUser"
import { Application as DouyinApplication } from "./douyin/application"
import { Application as WechatApplication } from "./wechat/application"
import { ProxyRequest } from "./proxyRequest"

export const PlatformFactory = (opt) => {
  const requestFactory = opt.requestFactory

  return {
    create: (platformAccountUser) => {
      const accountUser = new AccountUser(platformAccountUser)
      
      let platformApplication

      switch (accountUser.source) {
        case PLATFORM_TYPE.DOUYIN:
          platformApplication = new DouyinApplication(accountUser)
          break;
        case PLATFORM_TYPE.WECHAT:
          platformApplication = new WechatApplication(accountUser)
          break;
        default:
          throw new PlatformNotSupportError('platform not support')
      }

      const proxyRequest = new ProxyRequest(requestFactory, accountUser)
      platformApplication.setRequest(proxyRequest)

      return platformApplication
    }
  }
}